﻿package plugin.lianzhiexp
{
    import __AS3__.vec.*;
    import com.core.*;
    import com.sh.game.display.*;
    import com.sh.game.util.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.utils.*;
    import manager.*;
    import morn.core.components.*;
    import uiresource.*;
    import view.message.*;

    public class LianzhiexpUICtrl extends LianzhiUI
    {
        private var _instance:Lianzhiexp;
        private var datatimes:int;
        private var counttimes:int = 5;
        private var userexp:int;
        private var lianzhigonfig:Object;
        private var msgArr:Array;
        public var showIndex:int = 0;
        public var itemCount:int = 3;
        public var txtArr:Vector.<Label>;
        public var firstTxt:Label;
        public var maxIndex:int;
        public var changeNext:int = -1;
        private var _tiemID1:uint = 0;
        private var benciexp:int;
        private var huafeimoney:int;
        private var _serverData:Object;
        private var _letime:int;
        private var _timeID:uint = 0;
        private var lianzhiBtnX:int;
        private var lianzhiBtnY:int;

        public function LianzhiexpUICtrl(param1:Lianzhiexp)
        {
            this.lianzhigonfig = ConfigDictionary.data.lianzhiexp;
            this.msgArr = [];
            this.txtArr = new Vector.<Label>;
            this._instance = param1;
            return;
        }// end function

        override protected function createChildren() : void
        {
            loadUI("Lianzhi.xml");
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            closeBtn.addEventListener(MouseEvent.CLICK, this.OnClose);
            XMLMcManager.instance.complatefuncs["lianzhiexp"] = this.mccompleteFunc;
            var _loc_1:* = Control.create(LianzhiexpControl, this);
            ModelProxy.addObserver("lianzhiexp", _loc_1);
            var _loc_2:* = UserData.getOnlineData();
            this.datatimes = _loc_2.lianzhitimes;
            this.initpanel();
            this.infodata();
            return;
        }// end function

        public function huojiangmsg(param1:Object) : void
        {
            if (!param1)
            {
                return;
            }
            this.msgArr = [];
            var _loc_2:* = param1.info;
            var _loc_3:int = 0;
            while (_loc_3 < _loc_2.length)
            {
                
                this.msgArr.push("玩家 <font color =\'#218a00\'>" + _loc_2[_loc_3].name + "</font> 在炼制经验时，天降鸿福，获得 <font color=\'#ef7510\'>" + _loc_2[_loc_3].num + "</font> 元宝");
                _loc_3++;
            }
            this.showRun();
            return;
        }// end function

        private function showRun() : void
        {
            var _loc_3:String = null;
            var _loc_4:Label = null;
            var _loc_1:int = 0;
            this.maxIndex = this.msgArr.length;
            this.itemCount = this.maxIndex;
            var _loc_2:int = 0;
            while (_loc_2 < this.itemCount)
            {
                
                _loc_3 = this.msgArr[_loc_2];
                _loc_4 = new Label();
                _loc_4.stroke = "0x0";
                _loc_4.color = "0xffe4c7";
                _loc_4.isHtml = true;
                _loc_4.text = _loc_3;
                _loc_4.width = 193;
                _loc_4.multiline = true;
                _loc_4.wordWrap = true;
                _loc_4.leading = 4;
                _loc_4.mouseChildren = false;
                _loc_4.mouseEnabled = false;
                box_run.addChild(_loc_4);
                _loc_4.y = _loc_1;
                _loc_1 = _loc_1 + (_loc_4.height + 5);
                this.txtArr.push(_loc_4);
                _loc_2++;
            }
            this.firstTxt = this.txtArr.shift();
            if (this.maxIndex > 2)
            {
                this.showIndex = 3;
                box_run.scrollRect = new Rectangle(0, 0, box_run.width, box_run.height);
                if (this._tiemID1 > 0)
                {
                    clearInterval(this._tiemID1);
                    this._tiemID1 = 0;
                }
                this._tiemID1 = setInterval(this.doRun, 40);
            }
            return;
        }// end function

        private function doRun() : void
        {
            var _loc_2:int = 0;
            var _loc_3:Label = null;
            var _loc_4:Label = null;
            var _loc_1:* = box_run.scrollRect;
            (_loc_1.y + 1);
            if (this.changeNext > 0)
            {
                var _loc_5:String = this;
                var _loc_6:* = this.changeNext + 1;
                _loc_5.changeNext = _loc_6;
                if (this.changeNext > 3)
                {
                    _loc_1.y = 0;
                    _loc_2 = 0;
                    this.firstTxt.y = _loc_2;
                    _loc_2 = _loc_2 + (this.firstTxt.height + 5);
                    for each (_loc_3 in this.txtArr)
                    {
                        
                        _loc_3.y = _loc_2;
                        _loc_2 = _loc_2 + (_loc_3.height + 5);
                    }
                    this.changeNext = -1;
                }
            }
            if (this.firstTxt.y + this.firstTxt.height <= _loc_1.y)
            {
                _loc_4 = this.txtArr.pop();
                this.firstTxt.y = _loc_4.y + _loc_4.height + 5;
                this.txtArr.push(_loc_4, this.firstTxt);
                this.firstTxt = this.txtArr.shift();
                var _loc_5:String = this;
                var _loc_6:* = this.showIndex + 1;
                _loc_5.showIndex = _loc_6;
                if (this.showIndex >= this.maxIndex)
                {
                    this.changeNext = 0;
                }
            }
            box_run.scrollRect = _loc_1;
            return;
        }// end function

        private function mccompleteFunc() : void
        {
            var _loc_1:* = XMLMcManager.instance.getPluginMc("lianzhiexp", "luzhi");
            _loc_1.mouseEnabled = false;
            _loc_1.mouseChildren = false;
            _loc_1.x = -85;
            _loc_1.y = -85;
            this.addChild(_loc_1);
            _loc_1.play();
            var _loc_2:* = XMLMcManager.instance.getPluginMc("lianzhiexp", "ding");
            _loc_2.mouseEnabled = false;
            _loc_2.mouseChildren = false;
            _loc_2.x = 190;
            _loc_2.y = -50;
            this.addChild(_loc_2);
            _loc_2.play();
            return;
        }// end function

        private function infodata() : void
        {
            var _loc_1:Object = null;
            for each (_loc_1 in this.lianzhigonfig)
            {
                
                if ((this.datatimes + 1) == _loc_1.times)
                {
                    this.benciexp = _loc_1.getexp;
                    this.huafeimoney = _loc_1.cost;
                }
            }
            huafeiLabel.text = "花费元宝：" + this.huafeimoney;
            if (this.counttimes - this.datatimes == 0)
            {
                timesLabel.color = MColor.New56;
                lianzhiBtn.disabled = true;
            }
            else
            {
                timesLabel.color = MColor.New62;
            }
            timesLabel.text = this.counttimes - this.datatimes + "/" + this.counttimes;
            this.Upexp();
            return;
        }// end function

        public function Upexp() : void
        {
            var _loc_6:int = 0;
            var _loc_1:* = UserData.getUserData();
            this.userexp = _loc_1.exp;
            var _loc_2:* = this.userexp + this.benciexp;
            var _loc_3:* = ConfigDictionary.data.level;
            var _loc_4:int = 0;
            var _loc_5:* = UserData.getUserData("level");
            var _loc_7:* = UserData.getUserData("level");
            while (_loc_7 <= _loc_3.length)
            {
                
                if (_loc_2 >= _loc_3[_loc_7])
                {
                    _loc_5 = _loc_5 + 1;
                    _loc_2 = _loc_2 - _loc_3[_loc_7];
                }
                else
                {
                    break;
                }
                _loc_7++;
            }
            lvlist.array = this.fenjiarr(_loc_5);
            expLabel.text = this.benciexp.toString();
            return;
        }// end function

        public function kaifutimes(param1:Object) : void
        {
            this._serverData = param1;
            return;
        }// end function

        private function initpanel() : void
        {
            var _loc_1:* = int(ModelProxy.getValue("data", "openday")) + 1;
            var _loc_2:* = ModelProxy.getValue("data", "servertime") + getTimer();
            var _loc_3:* = new Date(_loc_2);
            var _loc_4:* = new Date();
            new Date().setTime(_loc_2 - (_loc_1 - 3) * 86400000);
            var _loc_5:* = new Date(_loc_4.fullYear, _loc_4.month, _loc_4.date, 23, 59, 59, 59);
            this._letime = (_loc_5.time - _loc_3.time) / 1000;
            if (this._letime >= 0)
            {
                this._timeID = setInterval(this.doTime, 1000);
            }
            else
            {
                jshiqulabel.visible = false;
            }
            lianzhiBtn.addEventListener(MouseEvent.CLICK, this.Onlianzhi);
            jiangjinLink.addEventListener(MouseEvent.MOUSE_OVER, this.Ontip);
            return;
        }// end function

        protected function Ontip(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "normal", "<font color = \'#997f60\'>1.玩家经验炼制花费的元宝将直接累计到奖池\n" + "2.每次进行经验炼制均有机会获得奖池1/10的元宝作为额外奖励</font>");
            jiangjinLink.addEventListener(MouseEvent.MOUSE_OUT, this.closeTips);
            return;
        }// end function

        protected function closeTips(event:MouseEvent) : void
        {
            ModelProxy.change("tips", "state", "close");
            jiangjinLink.removeEventListener(MouseEvent.MOUSE_OUT, this.closeTips);
            return;
        }// end function

        private function doTime() : void
        {
            if (this._letime == 0)
            {
                if (this._timeID > 0)
                {
                    clearInterval(this._timeID);
                    this._timeID = 0;
                }
                return;
            }
            var _loc_1:String = this;
            var _loc_2:* = this._letime - 1;
            _loc_1._letime = _loc_2;
            jshiqulabel.text = TimeDataFormatUtil.getLeftTime2(this._letime);
            return;
        }// end function

        protected function Onlianzhi(event:MouseEvent) : void
        {
            var event:* = event;
            var YBmoney:* = UserData.getBagData("money");
            if (YBmoney >= this.huafeimoney)
            {
                Alert2.show("你是否花费 " + MColor.getHMTL(this.huafeimoney + "", 6) + "" + " 元宝吗 ", "", ["确定", "取消"], function (param1:int, param2:Object) : void
            {
                if (param1 == 0)
                {
                    ModelProxy.change("data", "lianzhipiao", new Point(event.stageX + 200, event.stageY - 350));
                    ModelProxy.change("mainui", "lianzhipiaoSucceed", null);
                    ModelProxy.change("data", "recycle", 2);
                    ModelProxy.change("data", "lianzhiexpxy", new Point(event.stageX - 150, event.stageY - 80));
                    PluginManager.instance.sendToServer("lianzhiexp", 2, null);
                }
                return;
            }// end function
            );
            }
            else
            {
                WarningManager.show(42);
            }
            return;
        }// end function

        public function lianzhitimes(param1:int) : void
        {
            this.datatimes = param1;
            ModelProxy.change("tasktrack", "getonline", null);
            this.infodata();
            return;
        }// end function

        public function Countmoney(param1:int) : void
        {
            this.uprefresh(param1, false);
            return;
        }// end function

        public function Succeed(param1:int) : void
        {
            this.uprefresh(param1, true);
            return;
        }// end function

        private function uprefresh(param1:int, param2:Boolean) : void
        {
            var _loc_3:* = param1;
            if (_loc_3 < 10)
            {
                list.array = [{num:"png.common." + _loc_3}];
            }
            else
            {
                list.array = this.fenjiarr(_loc_3);
            }
            moneyLabel.text = "本次炼制有几率获得：" + String(Math.floor(_loc_3 / 10));
            return;
        }// end function

        private function fenjiarr(param1:int) : Array
        {
            var _loc_4:int = 0;
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            var _loc_8:int = 0;
            var _loc_9:int = 0;
            var _loc_10:String = null;
            var _loc_12:int = 0;
            var _loc_2:Array = [];
            var _loc_3:Array = [];
            if (param1 < 100)
            {
                _loc_4 = param1 % 10;
                _loc_5 = Math.floor(param1 / 10);
                _loc_10 = _loc_5 + "," + _loc_4;
            }
            if (param1 >= 100 && param1 < 1000)
            {
                _loc_4 = param1 % 10;
                _loc_12 = param1 % 100;
                _loc_5 = Math.floor(_loc_12 / 10);
                _loc_6 = Math.floor(param1 / 100);
                _loc_10 = _loc_6 + "," + _loc_5 + "," + _loc_4;
            }
            if (param1 >= 1000 && param1 < 10000)
            {
                _loc_4 = param1 % 10;
                _loc_5 = Math.floor(param1 % 100 / 10);
                _loc_6 = Math.floor(param1 % 1000 / 100);
                _loc_7 = Math.floor(param1 / 1000);
                _loc_10 = _loc_7 + "," + _loc_6 + "," + _loc_5 + "," + _loc_4;
            }
            if (param1 >= 10000 && param1 < 100000)
            {
                _loc_4 = param1 % 10;
                _loc_5 = Math.floor(param1 % 100 / 10);
                _loc_6 = Math.floor(param1 % 1000 / 100);
                _loc_7 = Math.floor(param1 % 10000 / 1000);
                _loc_8 = Math.floor(param1 / 10000);
                _loc_10 = _loc_8 + "," + _loc_7 + "," + _loc_6 + "," + _loc_5 + "," + _loc_4;
            }
            if (param1 >= 100000 && param1 < 1000000)
            {
                _loc_4 = param1 % 10;
                _loc_5 = Math.floor(param1 % 100 / 10);
                _loc_6 = Math.floor(param1 % 1000 / 100);
                _loc_7 = Math.floor(param1 % 10000 / 1000);
                _loc_8 = Math.floor(param1 % 100000 / 10000);
                _loc_9 = Math.floor(param1 / 100000);
                _loc_10 = _loc_9 + "," + _loc_8 + "," + _loc_7 + "," + _loc_6 + "," + _loc_5 + "," + _loc_4;
            }
            _loc_2 = _loc_10.split(",");
            var _loc_11:int = 0;
            while (_loc_11 < _loc_2.length)
            {
                
                _loc_3.push({num:"png.common." + _loc_2[_loc_11]});
                _loc_11++;
            }
            return _loc_3;
        }// end function

        protected function OnClose(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        private function close() : void
        {
            if (this._tiemID1 > 0)
            {
                clearInterval(this._tiemID1);
                this._tiemID1 = 0;
            }
            this._instance.close();
            return;
        }// end function

    }
}
